package com.gson.util;

public class StringUtil {
	/**
	 * 简单检测参数有效性
	 */
	public static boolean testParam(String s) {
		return null != s && s.trim().length() > 0;
	}

	/**
	 * 简单检测参数有效性,等于eqStr的字符
	 */
	public static boolean testParam(String s, String eqStr) {
		return null != s && eqStr.equals(s) && s.trim().length() > 0;
	}

	/**
	 * 获取最小区划名称
	 */
	public static String getCity(String s) {
		String city = null;
		if (s != null && s.trim().length() != 0) {
			String[] cityArray = s.split("\\|\\|");
			int index = cityArray.length - 1;
			city = cityArray[index];
		}
		return city;
	}

	public static String filter(String str) {
		if (str == null) {
			str = "";
		}
		if (str.equals("null"))
			str = "";
		if (str.equals("undefined"))
			str = "";
		return str.trim();
	}
	
	public static String getMinRegion(String cityCode){
		// 判断是否为空
		if(testParam(cityCode)){
			// 判断城市代码长度
			if(cityCode.length()==6){
				if (cityCode.substring(2, 6).equals("0000")) {// 省级
					return cityCode.substring(0, 2);
				} else if (cityCode.substring(4, 6).equals("00")) {// 市级
					return cityCode.substring(0, 4);
				} else {// 县一级
					return cityCode;
				}	
			} else {
				return cityCode;
			}
		} else {
			return filter(cityCode);
		}
	}
	
	public static String replaceVerticalLine(String string){
		String result = "";
		result = filter(string);
		if(result.indexOf("|")>-1) {
			result = result.replace("|", "");
		}
		return result;
	}
	
	/**
	 * 简单检测字符串是否以beginStr开头
	 */
	public static boolean startWith(String s, String beginStr) {
		return null != s && s.startsWith(beginStr);
	}
	
	/**
	 * 去除字符串中左端的beginStr
	 */
	public static String ltrim(String s, String beginStr) {
		if(testParam(s)&&testParam(beginStr)&&startWith(s, beginStr)){
			return s.substring(beginStr.length(), s.length());
		}
		return s;
	}
	
}
